package com.jianshuge.app.ui;

import com.jianshuge.app.AppContext;
import com.jianshuge.app.AppException;
import com.jianshuge.app.R;

import com.jianshuge.app.bean.MyResult;
import com.jianshuge.app.common.StringUtils;
import com.jianshuge.app.common.UIHelper;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import android.widget.Button;

import android.widget.EditText;
import android.widget.ImageView;

public class BooklistPub extends BaseActivity{

	private ImageView mBack;
	private EditText mTitle;
	private EditText mContent;
	private Button mPublish;
    private ProgressDialog mProgress;

	private InputMethodManager imm;
	private int cur_booklist_id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.booklist_pub);
		
		this.initView();
		
	}
	
    //初始化视图控件
    private void initView()
    {    	
    	
		cur_booklist_id = getIntent().getIntExtra("booklist_id", 0);
    	imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    	mBack = (ImageView)findViewById(R.id.booklist_pub_back);
    	mPublish = (Button)findViewById(R.id.booklist_pub_publish);
    	mTitle = (EditText)findViewById(R.id.booklist_pub_title);
    	mContent = (EditText)findViewById(R.id.booklist_pub_content);
    	
    	mBack.setOnClickListener(UIHelper.finish(this));
    	mPublish.setOnClickListener(publishClickListener);
    	
		if( cur_booklist_id > 0) {
			mTitle.setText( getIntent().getStringExtra("booklist_title") );
		}
		
    	//编辑器添加文本监听
 //   	mTitle.addTextChangedListener(UIHelper.getTextWatcher(this, AppConfig.TEMP_POST_TITLE));
//    	mContent.addTextChangedListener(UIHelper.getTextWatcher(this, AppConfig.TEMP_POST_CONTENT));
    	
    	//显示临时编辑内容
 //   	UIHelper.showTempEditContent(this, mTitle, AppConfig.TEMP_POST_TITLE);
 //   	UIHelper.showTempEditContent(this, mContent, AppConfig.TEMP_POST_CONTENT);
    	//显示临时选择分类
//    	String position = ((AppContext)getApplication()).getProperty(AppConfig.TEMP_POST_CATALOG);
    }
    
	private View.OnClickListener publishClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			//隐藏软键盘
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
			
			final String title = mTitle.getText().toString();
			if(StringUtils.isEmpty(title)){
				UIHelper.ToastMessage(v.getContext(), "请输入书单名称");
				return;
			}
			
			final String content = mContent.getText().toString();
			if(StringUtils.isEmpty(content)){
			//	UIHelper.ToastMessage(v.getContext(), "请输入书单的描述");
			//	return;
			}
			
			final AppContext ac = (AppContext)getApplication();
			if(!ac.isLogin()){
				UIHelper.showLoginDialog(BooklistPub.this);
				return;
			}
			
			mProgress = ProgressDialog.show(v.getContext(), null, "发布中···",true,true); 
					
			final Handler handler = new Handler(){
				public void handleMessage(Message msg) {
					if(mProgress!=null)mProgress.dismiss();
					if(msg.what == 1){
						MyResult res = (MyResult)msg.obj;
						UIHelper.ToastMessage(BooklistPub.this, res.getErrorMessage());
						if(res.OK()){							
							Intent intent = new Intent();
							intent.putExtra("message", res.getErrorMessage());
							setResult(RESULT_OK, intent);
							finish();
						}
					}
					else {
						((AppException)msg.obj).makeToast(BooklistPub.this);
					}
				}
			};
			new Thread(){
				public void run() {
					Message msg = new Message();
					MyResult res = null;
					try {
						
						if( cur_booklist_id > 0)
							res = ac.updateBooklist(ac.getLoginUid(), cur_booklist_id, title, content);
						else
							res = ac.pubBooklist(ac.getLoginUid(), title, content);
						
						msg.what = 1;
						msg.obj = res;
		            } catch (AppException e) {
		            	e.printStackTrace();
						msg.what = -1;
						msg.obj = e;
		            }
					handler.sendMessage(msg);
				}
			}.start();
			
		}
	};
}
